import store from "@/store/index.js";

function hasPerm(value, el = false) {
  if (!Array.isArray(value)) {
    throw new Error(`需要配置权限,例如v-perm=['']`);
  }
  const userPerms = JSON.parse(JSON.stringify(store.state.perms))
  const hasAuth = value.findIndex((v) => userPerms.includes(v)) != -1;
  if (el && !hasAuth) {
    el.parentNode && el.parentNode.removeChild(el);
  }
  return hasAuth;
}

export default {
  install(app) {
    // 全局自定义v-permission指令
    app.directive("perm", {
      mounted(el, binding) {
        hasPerm(binding.value, el);
      },
    });
  },
};
